<?php
namespace App\Http\Controllers\Admin;

use App\DBs\Admin\MeetDB;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class MeetController extends BaseController
{
    public function __construct()
    {
        $this->dbMeet = new MeetDB();
    }

    //列表
    public function getIndex(Request $request)
    {
        $res = $this->dbMeet->getIndex();
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //新增
    public function setAdd(Request $request)
    {
        $arys = $request->all();
        $title = isset($arys['title']) && $arys['title'] ? $arys['title'] : '';
        $img = isset($arys['img']) && $arys['img'] ? $arys['img'] : '';
        $jointime = isset($arys['jointime']) && $arys['jointime'] ? $arys['jointime'] : '';
        if (!$title || !$img || !$jointime) {
            return $this->toJson(-2, '没有图片或参会时间');
        }
        $upData = ['title'=>$title,'jointime' => $jointime];
        if ($img) $upData['img'] = Upload::getPathByBase64($img);
        $res = $this->dbMeet->setAdd($upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //一个
    public function getOne(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        $res = $this->dbMeet->getOne($id);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //新增
    public function setUpdate(Request $request)
    {
        $arys = $request->all();
        $title = isset($arys['title']) && $arys['title'] ? $arys['title'] : '';
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        $img = isset($arys['img']) && $arys['img'] ? $arys['img'] : '';
        $jointime = isset($arys['jointime']) && $arys['jointime'] ? $arys['jointime'] : '';
        if (!$id || !$title || !$jointime) {
            return $this->toJson(-2, '没有图片或参会时间');
        }
        $upData = ['title'=>$title,'jointime' => $jointime];
        if ($img) $upData['img'] = Upload::getPathByBase64($img);
        $res = $this->dbMeet->setUpdate($id,$upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //删除
    public function setDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        if (!$id) {
            return $this->toJson(-2, '没有参数');
        }
        $res = $this->dbMeet->setDel($id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //大会选项
    public function getOption(Request $request)
    {
        $arys = $request->all();
        $selid = isset($arys['selid']) && $arys['selid'] ? $arys['selid'] : 0;
        $res = $this->dbMeet->getOption($selid);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //主题列表
    public function getFocusIndex(Request $request)
    {
        $arys = $request->all();
        $meetid = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $res = $this->dbMeet->getFocusIndex($meetid);
        if ($res['code']!=0) {
            return $this->toJson(-2,$res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //百度编辑器上传
    public function setFocusUploadConfig(Request $request)
    {
        $res = file_get_contents('ueditor.json');
        $res = json_decode($res,true);
//        echo '<pre>'; var_dump($res);
        $res['imageUrlPrefix'] = env('DOMAIN').'/';
        return $res;
    }

    //百度编辑器上传
    public function setFocusUpload(Request $request)
    {
        $file = Upload::uploadBdFile($request,'upfile');
        if ($file['code']!=0) {
            return $this->toJson(-2,$file['msg']);
        }
        $filePath = $file['data'];
//        return $this->toJson(0,'success',$filePath);
        return array('state'=>'SUCCESS','msg'=>'success','url'=>$filePath);
    }

    //主题新增
    public function setFocusAdd(Request $request)
    {
        $arys = $request->all();
        $meet = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $title = isset($arys['title']) && $arys['title'] ? $arys['title'] : '';
        $detail = isset($arys['detail']) && $arys['detail'] ? $arys['detail'] : '';
        $img = isset($arys['img']) && $arys['img'] ? $arys['img'] : '';
        $starttime = isset($arys['starttime']) && $arys['starttime'] ? $arys['starttime'] : '';
        if (!$meet || !$title || !$detail || !$starttime) {
            return $this->toJson(-2, '参数不全');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $upData = ['meetid'=>$meet,'title'=>$title,'detail'=>$detail,'starttime'=>$starttime];
        if ($img) $upData['img'] = Upload::getPathByBase64($img);
        $res = $this->dbMeet->setFocusAdd($upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //主题获取
    public function getFocusOne(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        if (!$id) {
            return $this->toJson(-2, '没有参数');
        }
        $res = $this->dbMeet->getFocusOne($id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0,'success',$res['data']);
    }

    //主题修改
    public function setFocusUpdate(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        $meet = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $title = isset($arys['title']) && $arys['title'] ? $arys['title'] : '';
        $detail = isset($arys['detail']) && $arys['detail'] ? $arys['detail'] : '';
        $img = isset($arys['img']) && $arys['img'] ? $arys['img'] : '';
        $starttime = isset($arys['starttime']) && $arys['starttime'] ? $arys['starttime'] : '';
        if (!$id ||!$meet || !$title || !$detail || !$starttime) {
            return $this->toJson(-2, '参数不全');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $upData = ['meetid'=>$meet,'title'=>$title,'detail'=>$detail,'starttime'=>$starttime];
        if ($img) $upData['img'] = Upload::getPathByBase64($img);
        $res = $this->dbMeet->setFocusUpdate($id,$upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //主题删除
    public function setFocusDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        if (!$id) {
            return $this->toJson(-2, '没有参数');
        }
        $res = $this->dbMeet->setFocusDel($id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //嘉宾列表
    public function getSpeakerIndex(Request $request)
    {
        $arys = $request->all();
        $meetid = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $res = $this->dbMeet->getSpeakerIndex($meetid);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, 'success', $res['data']);
    }

    //主题新增
    public function setSpeakerAdd(Request $request)
    {
        $arys = $request->all();
        $meet = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $uname = isset($arys['uname']) && $arys['uname'] ? $arys['uname'] : '';
        $udetail = isset($arys['udetail']) && $arys['udetail'] ? $arys['udetail'] : '';
        $uhead = isset($arys['uhead']) && $arys['uhead'] ? $arys['uhead'] : '';
        if (!$meet || !$uname || !$udetail || !$uhead) {
            return $this->toJson(-2, '参数不全');
        }
        $upData = ['meetid'=>$meet,'uname'=>$uname,'udetail'=>$udetail];
        if ($uhead) $upData['uhead'] = Upload::getPathByBase64($uhead);
        $res = $this->dbMeet->setSpeakerAdd($upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //主题删除
    public function setSpeakerDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        if (!$id) {
            return $this->toJson(-2, '没有参数');
        }
        $res = $this->dbMeet->setSpeakerDel($id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //大会章程获取
    public function getWay(Request $request)
    {
        $res = file_get_contents('dhway.json');
        if (!$res) {
            $res = ['cn'=>'大会章程','title'=>'','detail'=>''];
        } else {
            $res = json_decode($res,true);
        }
        $res['detail'] = json_decode($res['detail'],true);
        return $this->toJson(0,'success',$res);
    }

    //大会章程修改
    public function setWay(Request $request)
    {
        $arys = $request->all();
        $title = isset($arys['title'])&&$arys['title'] ? $arys['title'] : '';
        $detail = isset($arys['detail'])&&$arys['detail'] ? $arys['detail'] : '';
        if (!$title || !$detail) {
            return $this->toJson(-2,'章程未填写');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $res = ['cn'=>'大会章程','title'=>$title,'detail'=>$detail];
        $json = json_encode($res,384);
        file_put_contents('dhway.json',$json);
        return $this->toJson(0);
    }

    //大会报名获取
    public function getSignup(Request $request)
    {
        $res = file_get_contents('dhsignup.json');
        if (!$res) {
            $res = ['cn'=>'大会报名','detail'=>''];
        } else {
            $res = json_decode($res,true);
        }
        $res['detail'] = json_decode($res['detail'],true);
        return $this->toJson(0,'success',$res);
    }

    //大会报名修改
    public function setSignup(Request $request)
    {
        $arys = $request->all();
        $detail = isset($arys['detail'])&&$arys['detail'] ? $arys['detail'] : '';
        if (!$detail) {
            return $this->toJson(-2,'报名未填写');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $res = ['cn'=>'大会报名','detail'=>$detail];
        $json = json_encode($res,384);
        file_put_contents('dhsignup.json',$json);
        return $this->toJson(0);
    }

    //大会奖项获取
    public function getAward(Request $request)
    {
        $res = file_get_contents('dhaward.json');
        if (!$res) {
            $res = ['cn'=>'大会报名','detail'=>''];
        } else {
            $res = json_decode($res,true);
        }
        $res['detail'] = json_decode($res['detail'],true);
        return $this->toJson(0,'success',$res);
    }

    //大会奖项修改
    public function setAward(Request $request)
    {
        $arys = $request->all();
        $detail = isset($arys['detail'])&&$arys['detail'] ? $arys['detail'] : '';
        if (!$detail) {
            return $this->toJson(-2,'奖项未填写');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $res = ['cn'=>'大会报名','detail'=>$detail];
        $json = json_encode($res,384);
        file_put_contents('dhaward.json',$json);
        return $this->toJson(0);
    }

    //安全伦理获取
    public function getSafety(Request $request)
    {
        $res = file_get_contents('dhlunli.json');
        if (!$res) {
            $res = ['cn'=>'安全伦理','detail'=>''];
        } else {
            $res = json_decode($res,true);
        }
        $res['detail'] = json_decode($res['detail'],true);
        return $this->toJson(0,'success',$res);
    }

    //安全伦理修改
    public function setSafety(Request $request)
    {
        $arys = $request->all();
        $detail = isset($arys['detail'])&&$arys['detail'] ? $arys['detail'] : '';
        if (!$detail) {
            return $this->toJson(-2,'未填写');
        }
        $detail = json_encode($detail,JSON_UNESCAPED_UNICODE);
        $res = ['cn'=>'安全伦理','detail'=>$detail];
        $json = json_encode($res,384);
        file_put_contents('dhlunli.json',$json);
        return $this->toJson(0);
    }

    //资料列表
    public function getFileIndex(Request $request)
    {
        $arys = $request->all();
        $meetid = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $res = $this->dbMeet->getFileIndex($meetid);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, 'success', $res['data']);
    }

    //资料上传
    public function setFileUpload(Request $request)
    {
        $resFile = Upload::uploadFile($request);
        if ($resFile['code']!=0) {
            echo '<script>alert(\''.$resFile['msg'].'\');history.go(-1);</script>'; exit;
        }
        $file = $request->file('file');
        $fname = $file->getClientOriginalName();
        $fsize = $file->getClientSize();
        $fext = $file->getClientOriginalExtension();
        $upData = [
            'forigin'=>$fname, 'fext'=>$fext,
            'link'=>$resFile['data'], 'fsize'=>$fsize,
        ];
        $res = $this->dbMeet->setFileAdd($upData);
        if ($res['code']!=0) {
            echo '<script>alert(\'上传错误\');history.go(-1);</script>'; exit;
        }
        return redirect('/admin/meFileAdd.html?fid='.$res['data']);
    }

    //资料新增
    public function setFileAdd(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        $meet = isset($arys['meetid']) && $arys['meetid'] ? $arys['meetid'] : '';
        $fname = isset($arys['fname']) && $arys['fname'] ? $arys['fname'] : '';
        if (!$id || !$meet || !$fname) {
            return $this->toJson(-2, '参数不全');
        }
        $upData = ['meetid'=>$meet,'fname'=>$fname];
        $res = $this->dbMeet->setFileUpdate($id,$upData);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }

    //资料删除
    public function setFileDel(Request $request)
    {
        $arys = $request->all();
        $id = isset($arys['id']) && $arys['id'] ? $arys['id'] : '';
        if (!$id) {
            return $this->toJson(-2, '没有参数');
        }
        $res = $this->dbMeet->setFileDel($id);
        if ($res['code'] != 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0);
    }
}